<template>
  <div class="xtx-goods-page" v-if = "goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">{{goods.categories[1].name}}</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[0].id}`">{{goods.categories[0].name}}</XtxBreadItem>
        <XtxBreadItem to="/">{{goods.name}}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <!-- 图片与销量信息，两个小模块 -->
          <GoodsImage :image="goods.mainPictures"/>
          <GoodSale />
        </div>
        <div class="spec">
          <GoodName :goods="goods"/>
          <!-- skuId="300000026" -->
          <GoodsSku :goods="goods"  @change="changeText"/>
          <XtxNumbox v-model="count" min="1" max="10"/>
          <XtxButton size="middle" type="primary" style="margin:20px 0 0 10px" @click="insertCartFn">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId = "goods.id"/>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsTabs :goods="goods"/>
          </div>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <GoodsWarn />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1"/>
          <GoodsHot :type="2"/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsWarn from './components/goodsWarn.vue'
import GoodsHot from './components/goodsHot.vue'
import { useRoute } from "vue-router"
import GoodsRelevant from "./components/goodsRelevant.vue"
import GoodsSku from './components/goodsSku.vue'
import { provide, ref, watch } from 'vue'
import { findGoods } from '@/api/goods'
import GoodSale from './components/goodsSale.vue'
import GoodsImage from './components/goodsImg.vue'
import GoodName from './components/goodsName.vue'
import GoodsTabs from './components/goodsTabs.vue'
import store from '@/store'
import { useStore } from 'vuex'
import Message from '@/components/library/Message'
export default {
  name: "XtxGoodsPage",
  components: { GoodsRelevant,GoodSale, GoodsImage, GoodName, GoodsSku, GoodsTabs, GoodsHot, GoodsWarn },
  //   获取goods数据
  setup() {
    const count = ref(1)
    const goods = useGoods()
    const currSku = ref(null)
    const changeText = (item) => {
      if(item.skuId){
        goods.value.price = item.price
        goods.value.oldPrice = item.oldPrice,
        goods.value.inventory = item.inventory
        console.log(item,'item')
        currSku.value = item
        console.log(currSku.value,'current')
      }else{
        currSku.value = null
        
      }
    }
    // 依赖注入:所有后代组件都可以用(孙子组件曾孙子组件都可以用)
    provide('goods', goods)

    // 将获取的后台数据给action ->  需要的数据:id name picture price count skuId attrsText selected nowPrice stock isEffective
    const store = useStore()
    // const { skuId, specText:attrsText,nowPrice:price, inventory: stock } = currSku.value
    // const { name, mainPictures } = goods.value
    // console.log(currSku,'currSku')
    // console.log(goods,'goods')
    // console.log(skuId,'mainPictures')
    const insertCartFn = () => {
      if(currSku.value && currSku.value.skuId){
          // store.dispatch('cart/insertCart',{
          // name,
          // picture:mainPictures[0], 
          // nowPrice:price,
          // count: count.value,
          // skuId,
          // attrsText,
          // selected: true,
          // stock,
          // isEffective:true
          store.dispatch('cart/insertCart', {
          id: goods.value.id,
          skuId: currSku.value.skuId,
          name: goods.value.name,
          picture: goods.value.mainPictures[0],
          price: currSku.value.price,
          nowPrice: currSku.value.price,
          count: count.value,
          attrsText: currSku.value.specText,
          selected: true,
          isEffective: true,
          stock: currSku.value.inventory
        }).then(()=>{
          Message({type:'success',text:'加入购物车成功'})
          console.log('加入购物车成功');
        })
      }else{
          Message({type:'error',text:'请选择完整规格'})
      }
      
          
    }
    return { goods, changeText, count, insertCartFn }
  },
};
// 获取后台数据的函数
const useGoods = () => {
  const route = useRoute()
  const goods = ref(null)
  // 跳转需要监听
  watch(()=>route.params.id, (newVal)=>{
    if(newVal && `/product/${newVal}` === route.path)
    findGoods(route.params.id).then(data=>{
       goods.value = null
        // 少一个nextTick，有bug,我倒要看看会出什么bug
      // console.log(data)
      goods.value = data.result
    })
  },{immediate:true})
  

  return goods
}
</script>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}

</style>
